﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BarberShop.Data;
using BarberShop.Model;
using BarberShop.Common;
using System.Linq.Expressions;

namespace BarberShop.BLL
{
    public class DeviceInfoBLL
    {
        /// <summary>
        /// 添加设备
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public static bool SaveDeviceInfo(DeviceInfo info)
        {
            using (DataClassesDataContext data = new DataClassesDataContext())
            {
                try
                {
                    if (info.ID == 0)
                        data.DeviceInfo.InsertOnSubmit(info);
                    else 
                    {
                        ModifyDeviceInfo(info);
                    }
                    data.SubmitChanges();
                    return true;
                }
                catch { return false; }
            }
        }
        /// <summary>
        /// 获取所有设备信息
        /// </summary>
        /// <param name="search"></param>
        /// <returns></returns>
        public static IList<DeviceInfo> GetDeviceList(DeviceInfoSearch search)
        {
            using (DataClassesDataContext data = new DataClassesDataContext())
            {
                Expression<Func<DeviceInfo, bool>> WhereExpr = PredicateExtensions.True<DeviceInfo>();
                if (!string.IsNullOrEmpty(search.Mac))
                {
                    WhereExpr = WhereExpr.And(x => x.MacAddress.Contains(search.Mac));
                }
                if (search.StoreId != -1)
                {
                    WhereExpr = WhereExpr.And(x => x.Stores_ID.Equals(search.StoreId));
                }
                var List = data.DeviceInfo.Where(WhereExpr).ToList();
                return List;
            }
        }

        /// <summary>
        /// 获取
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static DeviceInfo GetDeviceInfo(int id)
        {
            using (DataClassesDataContext data = new DataClassesDataContext())
            {
                var info = (from a in data.DeviceInfo where a.ID == id select a).FirstOrDefault();
                return info;
            }
        }
        /// <summary>
        /// 获取门店ID
        /// </summary>
        /// <param name="mac"></param>
        /// <returns></returns>
        public static int GetStoreIdByMac(string mac)
        {
            using (DataClassesDataContext data = new DataClassesDataContext())
            {
                var info = (from a in data.DeviceInfo where a.MacAddress == mac select a).FirstOrDefault();
                if (info != null)
                {
                    int id = info.Stores_ID.HasValue ? info.Stores_ID.Value : 0;
                    return id;
                }
                return 0;
            }
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public static bool ModifyDeviceInfo(DeviceInfo info)
        {
            using (DataClassesDataContext data = new DataClassesDataContext())
            {
                try
                {
                    var r = (from a in data.DeviceInfo where a.ID == info.ID select a).FirstOrDefault();
                    r.MacAddress = info.MacAddress;
                    r.Stores_ID = info.Stores_ID;
                    data.SubmitChanges();
                    return true;
                }
                catch { return false; }
            }
        }

        /// <summary>
        /// 设置设备状态
        /// </summary>
        /// <param name="id"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        public static bool SetDeviceStatus(string mac, int status)
        {
            using (DataClassesDataContext data = new DataClassesDataContext())
            {
                try
                {
                    var r = (from a in data.DeviceInfo where a.MacAddress == mac select a).FirstOrDefault();
                    if (r != null)
                    {
                        r.LineStatus = status;
                        data.SubmitChanges();
                        return true;
                    }
                    return false;
                }
                catch { return false; }
            }
        }
    }
}
